﻿using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;

namespace Developmentalmadness.Web.UI.AjaxAbstractions
{
    /// <summary>
    /// A wrapper class for System.Web.UI.ScriptManager which is used by ScriptManagerBase
    /// when a System.Web.UI.ScriptManager server control exists on the page.
    /// </summary>
    internal class ScriptManagerWrapper : ScriptManagerBase
    {
        private ScriptManager _scriptManager;
        private ClientScriptManagerBase _clientScript;

        public ScriptManagerWrapper(ScriptManager scriptManager)
        {
            _scriptManager = scriptManager;
            _clientScript = new ClientScriptManagerWrapper(_scriptManager.Page);
        }

        public override ClientScriptManagerBase ClientScript
        {
            get
            {
                return _clientScript;
            }
        }

        #region IPostBackDataHandler Members

        public override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            return ((IPostBackDataHandler)_scriptManager).LoadPostData(postDataKey, postCollection);
        }

        public override void RaisePostDataChangedEvent()
        {
            ((IPostBackDataHandler)_scriptManager).RaisePostDataChangedEvent();
        }

        #endregion

        #region ScriptManager Members

        public override void RegisterArrayDeclaration(Control control, string arrayName, string arrayValue)
        {
            ScriptManager.RegisterArrayDeclaration(control, arrayName, arrayValue);
        }

        public override void RegisterClientScriptBlock(Control control, Type type, string key, string script, bool addScriptTags)
        {
            ScriptManager.RegisterClientScriptBlock(control, type, key, script, addScriptTags);
        }

        public override void RegisterClientScriptInclude(Control control, Type type, string key, string url)
        {
            ScriptManager.RegisterClientScriptInclude(control, type, key, url);
        }

        public override void RegisterClientScriptResource(Control control, Type type, string resourceName)
        {
            ScriptManager.RegisterClientScriptResource(control, type, resourceName);
        }

        public override void RegisterDispose(Control control, string disposeScript)
        {
            _scriptManager.RegisterDispose(control, disposeScript);
        }

        public override void RegisterExpandoAttribute(Control control, string controlId, string attributeName, string attributeValue, bool encode)
        {
            ScriptManager.RegisterExpandoAttribute(control, controlId, attributeName, attributeValue, encode);
        }

        public override void RegisterHiddenField(Control control, string hiddenFieldName, string hiddenFieldValue)
        {
            ScriptManager.RegisterHiddenField(control, hiddenFieldName, hiddenFieldValue);
        }

        public override void RegisterOnSubmitStatement(Control control, Type type, string key, string script)
        {
            ScriptManager.RegisterOnSubmitStatement(control, type, key, script);
        }

        public override void RegisterPostBackControl(Control control)
        {
            _scriptManager.RegisterPostBackControl(control);
        }

        public override void RegisterStartupScript(Control control, Type type, string key, string script, bool addScriptTags)
        {
            ScriptManager.RegisterStartupScript(control, type, key, script, addScriptTags);
        }

        public override void SetFocus(string clientID)
        {
            _scriptManager.SetFocus(clientID);
        }

        public override void SetFocus(Control control)
        {
            _scriptManager.SetFocus(control);
        }

        public override bool IsInAsyncPostBack
        {
            get { return _scriptManager.IsInAsyncPostBack; }
        }

        public override bool SupportsPartialRendering
        {
            get { return _scriptManager.SupportsPartialRendering; }
        }

        public override void RegisterAsyncPostBackControl(Control control)
        {
            _scriptManager.RegisterAsyncPostBackControl(control);
        }

        public override void RegisterExtenderControl<TExtenderControl>(TExtenderControl extenderControl, Control targetControl)
        {
            _scriptManager.RegisterExtenderControl<TExtenderControl>(extenderControl, targetControl);
        }

        public override void RegisterProxy(ScriptManagerProxy proxy)
        {
            throw new NotImplementedException();
        }

        public override void RegisterScriptControl<TScriptControl>(TScriptControl scriptControl)
        {
            _scriptManager.RegisterScriptControl<TScriptControl>(scriptControl);
        }

        public override void RegisterScriptDescriptors(IExtenderControl extenderControl)
        {
            _scriptManager.RegisterScriptDescriptors(extenderControl);
        }

        public override void RegisterScriptDescriptors(IScriptControl scriptControl)
        {
            _scriptManager.RegisterScriptDescriptors(scriptControl);
        }

        public override void RegisterUpdatePanel(UpdatePanel updatePanel)
        {
            throw new NotImplementedException();
        }

        public override void UnregisterUpdatePanel(UpdatePanel updatePanel)
        {
            throw new NotImplementedException();
        }

        public override string AsyncPostBackSourceElementID
        {
            get { return _scriptManager.AsyncPostBackSourceElementID; }
        }

        #endregion

    }
}
